<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Konversi_nilai extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_krs');
		$this->load->model('mdl_konversi_nilai');
		$this->load->model('mdl_mahasiswa');
		
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['kd_bobot'] = '';
		$this->load->view('mutasi_mhs/konversi_nilai',$data);
	}
	
	function getdata_matkul(){
		$kd_kurikulum = $this->input->post('kd_kurikulum');
		$kd_prodi = $this->input->post('kd_prodi');
		$id_tahunajaran = $this->input->post('id_tahunajaran');
		if($kd_kurikulum != ''){
			$data = $this->mdl_krs->getmatakuliah($kd_kurikulum, $kd_prodi, $id_tahunajaran);
			echo $this->mdl_krs->togrid($data['row_data'], $data['row_count']);	
		}else{echo json_encode(array('total'=>0, 'rows'=>array('kd_matakuliah'=>'')));}
	}
	
	function getsemester_mhs($nim){
		$res = $this->mdl_krs->getsemester_mhs($nim);
		echo json_encode(array('semester'=>$res));
	}
	
	function getdata_mhs(){
		$mhs = $this->mdl_mahasiswa->getdataByNIM($this->input->post('nim'));
		
		if($mhs->num_rows()>0){
			$data['nama'] = $mhs->row()->nama;
			$data['nama_prodi'] = $mhs->row()->nama_prodi;
			echo json_encode($data);
		}else{
			echo json_encode(array('error'=>true));
		}
	}
	
	function add(){
		$this->load->view('krs_form2');
	}
	
	function save(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		//echo $data['data']['rows'][10]['id_jadwal'];
		//return 1;
		
		# rules validasi form
		$this->form_validation->set_rules("kd_fakultas", 'Fakultas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Prodi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_kurikulum", 'Kurikulum', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("id_tahunajaran", 'Tahun Ajaran', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nim", 'NIM Mahasiswa', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_dosen", 'Pembimbing', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_prodi',' ',' '))==''?'':form_error('kd_prodi',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			// cek krs (kd_fakultas, kd_prodi, nim, id_tahunajaran)
			if($this->mdl_konversi_nilai->isExist_krs($data)){
				$data["pesan_error"] = 'Data dengan NIM "'.$data['nim'].'", sudah melakukan KRS!';
			}else{
				// ambil nomor
				$data['nomor'] = $this->mdl_konversi_nilai->getnomor();
				$result = $this->mdl_konversi_nilai->InsertOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}

	function load_kd_bobot(){
		$data = $this->mdl_konversi_nilai->load_kd_bobot();
		//$arr = array('id_jadwal'=>'0', 'nama_dosen'=>'');
		$arr = array();
		array_push($arr, array('kd_bobot'=>'', 'kd_bobot'=>'&nbsp;'));
		foreach($data['row_data']->result() as $row){
			array_push($arr, array('kd_bobot'=>$row->kd_bobot, 'kd_bobot'=>$row->kd_bobot));
		}
		echo json_encode($arr);
	}
	
	/*---------- KPRS -------------------------*/
	function kprs(){
		$this->load->view('kprs');
	}
	
	function load_krs(){
		$data = $this->mdl_krs->getdata_krs();
		echo $this->mdl_krs->togrid($data['row_data'], $data['row_count']);
	}
	
	function delete_krs_detail($id){
		$result = $this->mdl_krs->delete_krs_detail($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
	function add_kprs($id_krs, $kd_fakultas, $kd_prodi, $id_tahunajaran, $nim){
		// init data
		$data['id_krs'] = $id_krs;
		$data['kd_fakultas'] = $kd_fakultas;
		$data['kd_prodi'] = $kd_prodi;
		$data['id_tahunajaran'] = $id_tahunajaran;
		$data['nim'] = $nim;
		$this->load->view('kprs_form', $data);
	}
	
	function save_kprs(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		//echo $data['data']['rows'][10]['id_jadwal'];
		//return 1;
		
		# rules validasi form
		$this->form_validation->set_rules("id_krs", 'ID KRS', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_fakultas", 'Fakultas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Prodi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_kurikulum", 'Kurikulum', 'trim|required|xss_clean');
		$this->form_validation->set_rules("id_tahunajaran", 'Tahun Ajaran', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nim", 'NIM Mahasiswa', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_prodi',' ',' '))==''?'':form_error('kd_prodi',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			// cek matkul di detail. param(id_krs, kd_fakultas, kd_prodi, nim, id_tahunajaran, kd_matakuliah)
			// jika ada maka update status jadi A
			// kalo tidak ada Insert matkul baru ke detail krs
			foreach($data['data']['rows'] as $row){
				if($row['id_jadwal'] != ''){
					$data['kd_matakuliah'] = $row['kd_matakuliah'];
					$id_krs_detail = '';
					if($this->mdl_krs->isExist_detailkrs($data, $id_krs_detail)){
						$result = $this->mdl_krs->update_status_detailkrs($id_krs_detail, 'A');
					}else{
						$data['kd_matakuliah'] = $row['kd_matakuliah'];
						$data['id_jadwal'] = $row['id_jadwal'];
						$result = $this->mdl_krs->insert_detailkrs($data);
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
}